<?php
namespace Admin\Controller;
/**
 * 菜品列表
 * User: linkwhy
 * Date: 2015/10/16
 * Time: 14:07
 */
class GoodsController extends AdminController{

    public function index(){

        $class_id = I('get.class_id', 0);
        $keyword = I('get.keyword', '');
        $paixu = I('get.order', 'asc');
		$sj = I('get.sj', 1);       //默认上架
        $where = "is_sj = $sj";
        if($class_id > 0){
            $where .= " and class_id = $class_id";
        }
        if($keyword != ''){
            $where .= " and name like '%{$keyword}%'";
        }

        if($paixu == 'asc'){
            $order = "is_recommend desc";
            $paixu = "desc";
        }else{
            $order = "is_recommend asc";
            $paixu = "asc";
        }
		
		if($sj == 'asc'){
			$order = "is_recommend desc";
			$sj = 'desc';
		}else{
			$order = "is_recommend asc";
			$sj = 'asc';
		}

        if( !IS_ADMIN ){
            $where .= " and class_id = " . CLASS_ID;
        }
        $list   = $this->lists('goods', $where, $order);
        int_to_string($list);
        $dclass = M('goods_class');
        foreach($list as $Key => &$v){
            $data = $dclass->field('pid,name')->where(array('id' => $v['class_id']))->find();
            $v['class_name'] = $data['name'];
            unset($data);
        }
        $data['user_type'] = intval( USER_TYPE );

        $this->assign('_list', $list);
        $this->assign('paixu', $paixu);
		$this->assign('sj', $sj);
        $this->assign('data', $data);
        $this->assign('classes', $this->getAllgoods());
        $this->meta_title = '商品列表';
        $this->display();
    }

    public function add(){
        $typeid = I('get.typeid',1);
        $this->meta_title = '新增商品';
        $data['user_type'] = intval($_SESSION['onethink_admin']['user_auth']['user_type']);
        $this->assign('data', $data);
        $this->assign('_class', $this->getAllgoods());
        $this->units = $this->getUnits();
        $this->assign('_brand', $this->getList('brand', 'id,name'));
        $this->assign('typeid',$typeid);
        $this->assign('id', 0);
        $this->display('update');
    }

    public function edit(){
        $dclass = M('goods');
        $typeid = I('get.typeid',1);
        $id = I('get.id');
        switch($typeid){
            case 1:
                //商品基础信息
                $data = $dclass->field(true)->where(array('id' => $id))->find();
                break;
            case 2:
                //颜色
                $data = M('color')->field(true)->where(array('gid' => $id))->find();
                break;
            case 3:
                //尺寸
                $data = M('size')->field(true)->where(array('gid' => $id))->find();
                break;
            case 4:
                //相册
                $data = M('photos')->field(true)->where(array('gid' => $id))->find();
                break;
            case 5:
                //商品介绍
                $data = M('goods_introduce')->field(true)->where(array('gid' => $id))->find();
                $ginfo = M('goods_info');
                $list = $ginfo->field('id,name,type,field,values')->where("pid = 0")->select();
                foreach($list as $key => $val){
                    if($val['type'] == 2){
                        $list[$key]['temp'] = $ginfo->field('name')->where("pid={$val['id']}")->order('id ASC')->select();
                    }
                }
                $this->assign('list', $list);
                break;
        }
        $data['user_type'] = intval($_SESSION['onethink_admin']['user_auth']['user_type']);
        $this->meta_title = '修改商品';
        $this->units = $this->getUnits();
        $this->assign('data', $data);
        $this->assign('id', $id);
        $this->assign('typeid',$typeid);
        $this->assign('_class', $this->getAllgoods());
        $this->assign('_brand', $this->getList('brand', 'id,name'));
        $this->display('update');
    }

    public function export(){
        $this->display();
    }

    public function exportData(){
        if(isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0)){
            $result = $this->importExecl($_FILES["import"]["tmp_name"]);
            if($result["error"] == 1){
                $execl_data = $result["data"][0]["Content"];
                $addList = array();
                foreach($execl_data as $k=>$v){
                    if($k == 1){
                        continue;
                    }else{
                        $addList[] = array(
                            'name' => $v[0],
                            'class_id' => $v[1],
                            'brand_id' => $v[2],
                            'price' => $v[3],
                            'shop_price' => $v[4],
                            'zhekou' => $v[5],
                            'sale_count' => $v[6],
                            'is_sj' => $v[7],
                        );
                    }
                }
                if(M('goods')->addAll($addList)){
                    $this->success('导入成功！', U('index'));
                }else{
                    $this->success('导入失败！', U('index'));
                }
            }
        }
    }
}
